package org.lvgo;

import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @author lvgo
 * @version 1.0
 * @Description: CTS实现工厂模式
 * @date 18-8-24 下午3:57
 */
public interface CTS {

    static CTS init(Consumer<Peoples> consumer) {
        Map<String, Supplier<Persons>> map = new HashMap<>();
        consumer.accept(map::put);
        return person -> map.get(person).get();

    }

    Persons getPerson(String name);
}

interface Peoples {
    void come(String name, Supplier<Persons> personSupplier);
}

class TTT {
    public static void main(String[] args) {
        CTS cts = CTS.init(people -> {
            people.come("张三", () -> new Persons("张三"));
            people.come("李四", () -> new Persons("李四"));
            people.come("王五", () -> new Persons("王五"));
        });

        Persons person = cts.getPerson("王五");
        System.out.println("persons = " + person);
    }
}

class Persons {

    private String name;

    public Persons() {
    }

    public Persons(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        return new StringJoiner(", ", Persons.class.getSimpleName() + "[", "]")
                .add("name='" + name + "'")
                .toString();
    }
}



